package com.hky.front_end_monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.front_end_monitor.dto.ErrorDTO;
import com.hky.front_end_monitor.entity.SysErrorVital;
import com.hky.front_end_monitor.mapper.SysErrorVitalMapper;
import com.hky.front_end_monitor.service.SysErrorVitalService;
import com.hky.front_end_monitor.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hello
 * @description 针对表【sys_error_vital】的数据库操作Service实现
 * @createDate 2022-08-01 15:35:48
 */
@Service
@SuppressWarnings("unchecked")
public class SysErrorVitalServiceImpl extends ServiceImpl<SysErrorVitalMapper, SysErrorVital>
        implements SysErrorVitalService {

    @Resource
    SysErrorStackServiceImpl errorStackService;

    @Resource
    SysHttpRecordServiceImpl httpRecordService;

    // 这个只是用来存下pi信息
    @Resource
    SysUserVitalServiceImpl userVitalService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean storeErrorVital(Map map) {
        String userId = StringUtils.caseToString(map.get("userId"));
        SysErrorVital errorVital = new SysErrorVital();
        errorVital.setUserId(userId);
        String errUUID = UuidGen.getUUID();
        errorVital.setErrorVitalId(errUUID);
        errorVital.setMechaismType(StringUtils.caseToString(((Map) map.getOrDefault("mechanism", map)).getOrDefault("type", map)));
        errorVital.setErrType(StringUtils.caseToString(map.getOrDefault("type", map)));
        errorVital.setErrInfo(StringUtils.caseToString(map.getOrDefault("value", map)));
        errorVital.setErrFile(StringUtils.caseToString(((LinkedHashMap) map.getOrDefault("meta", map)).getOrDefault("file", map)));
        errorVital.setErrCol(NumberUtils.toInteger(((LinkedHashMap) map.getOrDefault("meta", map)).getOrDefault("col", map)));
        errorVital.setErrRow(NumberUtils.toInteger(((LinkedHashMap) map.getOrDefault("meta", map)).getOrDefault("row", map)));
        String userVitalUUID = UuidGen.getUUID();
        errorVital.setUserVtialId(userVitalUUID);
        userVitalService.storeUserVital(map, userVitalUUID);
        boolean httpRecord = true;
        if (StringUtils.caseToString(((Map) (map.getOrDefault("mechanism", map))).getOrDefault("type", map)).equals("http")) {
            String httpUUID = UuidGen.getUUID();
            errorVital.setErrHttpRecordId(httpUUID);
            httpRecord = httpRecordService.storeHttpRecord(httpUUID, map);
        }
        return httpRecord & errorStackService.storeErrorStack(errUUID, map) & this.save(errorVital);
    }

    /**
     * @param userId
     * @return
     */
    @Override
    public List<ErrorDTO> getErrorInfo(String userId) {
        LambdaQueryWrapper<SysErrorVital> errorVitalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        errorVitalLambdaQueryWrapper.eq(SysErrorVital::getUserId, userId);
        List<SysErrorVital> sysErrorVitals = this.list(errorVitalLambdaQueryWrapper);
        List<ErrorDTO> errorDTOs = sysErrorVitals.stream().map(this::errorDTOBuild).collect(Collectors.toList());
        return errorDTOs;
    }

    private ErrorDTO errorDTOBuild(SysErrorVital sysErrorVital) {
        ErrorDTO errorDTO = new ErrorDTO();
        errorDTO.setMechanismType(sysErrorVital.getMechaismType());
        errorDTO.setType(sysErrorVital.getErrType());
        errorDTO.setCol(sysErrorVital.getErrCol());
        errorDTO.setRow(sysErrorVital.getErrRow());
        errorDTO.setErrHttpRecord(httpRecordService.getById(sysErrorVital.getErrHttpRecordId()));
        errorDTO.setErrPageInfo(userVitalService.getById(sysErrorVital.getUserVtialId()));
        errorDTO.setInfo(sysErrorVital.getErrInfo());
        return errorDTO;
    }
}



